package net.edu_soft.ec.asset.service.impl;

import net.edu_soft.ec.asset.mapper.AdvertisingMapper;
import net.edu_soft.ec.asset.pojo.Advertising;
import net.edu_soft.ec.asset.pojo.AdvertisingExample;
import net.edu_soft.ec.asset.service.AdvertisingService;
import net.edu_soft.ec.common.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AdvertisingServiceImpl implements AdvertisingService {

    @Autowired
    private AdvertisingMapper advertisingMapper;


    @Value("${serviceIp}")
    private String imageip;

    @Value("${imagePath}")
    private String imagePath;

    @Override
    public int addAdvertising(MultipartFile advertisingurl,String name,Integer sortOrder) {
        //1.检验数据
        if (advertisingurl!=null && name!=null){
            Advertising advertising=new Advertising();
            //2.定义数据
            String filesurl="gjy/Advertising";//定义轮播图片存储路径：/home/image/Advertising
            String uuid= UUID.randomUUID().toString();
            String filename="Advertising_"+uuid+".jpg";//定义图片的文件名称
            //3.存入数据库
            advertising.setName(name);
            advertising.setUrl(imageip+filesurl+"/"+filename);
            advertising.setSortOrder(0);//默认状态
            advertising.setStatus(1);
            advertising.setCreateDate(new Date());
            System.out.println("准备存入数据库的信息："+advertising);
            if(advertisingMapper.insert(advertising)==1){
                //4.存入图片
                int iscun= FileUtils.getImgUrl(advertisingurl,imagePath+filesurl,filename);
                if (iscun!=0){
                    return 1;
                }
            }

        }
        return 0;
    }

    @Override
    public int deleteAdvertising(int advertisingId) {
        return advertisingMapper.deleteByPrimaryKey(advertisingId);
    }

    @Override
    public List<Advertising> selectAll() {
        AdvertisingExample advertisingExample=new AdvertisingExample();
        return  advertisingMapper.selectByExample(advertisingExample);
    }

    @Override
    public int upDateAdvertising(Advertising advertising,MultipartFile advertisingurl) {
        boolean isfile=false;
        String filesurl = null;
        String filename = null;
        String yurl[]=advertisingMapper.selectByPrimaryKey(advertising.getAdvertisingId()).getUrl().split("gjy");

        if (advertising.getAdvertisingId()!=null){
            if (advertisingurl!=null){
                //准备数据
                filesurl="gjy/Advertising";//定义图片存储路径
                String uuid= UUID.randomUUID().toString();
                filename="Advertising_"+uuid+".jpg";//定义图片的文件名称
                advertising.setUrl(imageip+filesurl+"/"+filename);
                isfile=true;
            }
            AdvertisingExample example = new AdvertisingExample();
            AdvertisingExample.Criteria createCriteria = example.createCriteria();
            createCriteria.andAdvertisingIdEqualTo(advertising.getAdvertisingId());
            if (advertisingMapper.updateByExampleSelective(advertising,example)!=0){
                if (isfile!=false){
                    //保存图片
                    FileUtils.getImgUrl(advertisingurl,imagePath+filesurl,filename);
                    //删除原图片
                    FileUtils.delete(imagePath+"gjy"+yurl[1]);

                }
                return 1;
            }
        }
        return 0;
    }
}
